package groovy

import com.gee.spring.cloud.common.core.domain.frame.CstpWrapper
import com.gee.spring.cloud.common.core.entity.center.config.Org
import com.gee.spring.cloud.common.core.entity.center.config.OrgCenter
import com.gee.spring.cloud.data.transform.core.util.MappingUtil
import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled

static CstpWrapper toInner(Org org, OrgCenter orgCenter, byte[] data){
    ByteBuf byteBuf = Unpooled.wrappedBuffer(data)
    CstpWrapper cstpWrapper = new CstpWrapper()
    cstpWrapper.setVersion((byte)0x01)
            .setTid((int)MappingUtil.getSateIdentity(orgCenter.getOrgId(), byteBuf.getShortLE(1)))
            .setSid((int)MappingUtil.getOrgCenterIdentity(orgCenter.getOrgId(), byteBuf.getIntLE(3)))
            .setDid((int)MappingUtil.getDevIdentity(orgCenter.getOrgId(), byteBuf.getIntLE(7)))
            .setBid((int)MappingUtil.getFrameBid(orgCenter.getOrgId(), byteBuf.getIntLE(11)))
            .setPackageNo(byteBuf.getIntLE(15))
            .setFlag((byte)0x00)
            .setOrgIdentity(org.getOrgIdentity())
            .setDate(byteBuf.getShortLE(24))
            .setTime(byteBuf.getIntLE(26))
            ;
    short length = byteBuf.getShortLE(30)
    byte[] dataArea = new byte[length];
    byteBuf.getBytes(32, dataArea);
    cstpWrapper.setLength(length)
            .setBody(dataArea);
    byteBuf.release();
    return cstpWrapper;
}
